package edu.mapi.ir.planning;
/**
 * @author ZP
 */
public class AStarCell implements Comparable<AStarCell>{

	protected int x;
	protected int y;
	protected double distance2target;
	protected double distance2origin;
	protected AStarCell parent;
	protected double rotation = 0;
	
	public AStarCell(int x, int y, double dtarget, double dorigin) {
		this.x = x;
		this.y = y;
		this.distance2origin = dorigin;
		this.distance2target = dtarget;
	}
	
	
	public String toString() {
		return "("+x+","+y+")";
	}
	
	public String debugString() {
		return "["+x+","+y+"] F = "+getCost()+" = "+distance2origin+" + "+distance2target + ", rot="+Math.toDegrees(getNeededRot());
	}

	public AStarCell getParent() {
		return parent;
	}

	public void setParent(AStarCell parent) {
		this.parent = parent;		
	}
	
	public double getCost() {
		
		return distance2origin+distance2target;//+10*getNeededRot();
	}
	
	public double getNeededRot() {
		if (parent == null)
			return Math.abs(getAngle());
		else {
			return Math.abs(parent.getAngle() - getAngle());
		}
	}
	
	@Override
	public boolean equals(Object obj) {
		if (obj instanceof AStarCell) {
			AStarCell cell = (AStarCell) obj;
			return x == cell.x && y == cell.y;			
		}
		return super.equals(obj);
	}
	
	public int compareTo(AStarCell o) {
		if (getCost() > o.getCost())
			return 1;
		else if (o.getCost() > getCost())
			return -1;
		return 0;
	}
	
	public double getAngle() {
		if (getParent() == null)
			return rotation;
		else {
			return Math.atan2(y - getParent().y, x - getParent().x);
		}
	}


	public void setRotation(double rotation) {
		this.rotation = rotation;
	}
}
